﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entidades;
using DAO;
using System.Drawing;

public partial class abmSala : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = "";
        llenarGrilla();
    }
    protected void btnAgregarSala_Click(object sender, EventArgs e)
    {
        try
        {
            Sala sala = new Sala();
            sala.Descripcion = txtDescripcion.Text;
            sala.Ubicacion = txtUbicacion.Text;
            SalaDAO.agregarSala(sala);
            pnlAlta.Visible = false;
            pnlListado.Visible = true;
            lblError.ForeColor = Color.Green;
            lblError.Text = "Actividad insertada correctamente";
            llenarGrilla();
            txtDescripcion.Text = "";
            txtUbicacion.Text = "";
   
        }
        catch(SystemException ex)
        {
            lblError.Text += ex.Message.ToString();
        }
    }
    protected void llenarGrilla()
    {
        try
        {
            dgvSala.DataSource = SalaDAO.obtenerSala(txtFiltroDescripcion.Text);
            dgvSala.DataKeyNames = new string[] { "Id" };
            dgvSala.DataBind();
        }
        catch (SystemException ex)
        {
            lblError.Text += ex.Message.ToString();
        }
    }

    protected void btnNuevaSala_Click(object sender, EventArgs e)
    {
        pnlAlta.Visible = true;
        pnlListado.Visible = false;
        btnAgregarSala.Visible = true;
        btnModificarSala.Visible = false;
        txtFiltroDescripcion.Text = "";
    }
    protected void btnFiltrar_Click(object sender, EventArgs e)
    {
        try
        {
            llenarGrilla();
        }
        catch (SystemException ex)
        {
            lblError.Text = ex.Message;
        }
    }
    protected void btnModificarSala_Click(object sender, EventArgs e)
    {
        try
        {
            Sala sala = new Sala();
            sala.Id = int.Parse(txtId.Value);
            sala.Descripcion = txtDescripcion.Text;
            sala.Ubicacion = txtUbicacion.Text;
            SalaDAO.modificarSala(sala);
            pnlAlta.Visible = false;
            llenarGrilla();
            txtId.Value = "";
            txtUbicacion.Text = "";
            txtDescripcion.Text = "";

        }
        catch (SystemException ex)
        {
            lblError.Text += ex.Message.ToString();
        }
    }
    protected void btnEliminar_Command(object sender, CommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        int idSala = int.Parse(dgvSala.DataKeys[index].Value.ToString());
        try
        {
            if (e.CommandName == "Modificar")
            {
                pnlAlta.Visible = true;
                Sala sala = SalaDAO.obtenerSalaId(idSala);
                txtId.Value = sala.Id.ToString();
                txtDescripcion.Text = sala.Descripcion;
                txtUbicacion.Text = sala.Ubicacion;
                btnModificarSala.Visible = true;
                btnAgregarSala.Visible = false;
            }
            if (e.CommandName == "Eliminar")
            {
                SalaDAO.bajaSala(idSala);
                llenarGrilla();

            }
        }
        catch (SystemException ex)
        {
            lblError.Text += ex.Message.ToString();
        }
    }
    protected void dgvSala_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgvSala.PageIndex = e.NewPageIndex;
        dgvSala.DataBind();
    }
}